package dao.impl;

import dao.GoodsDao;
import entity.Goods;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    //上架
    public boolean insertGoods(Goods goods) {
        try {
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\Goods.txt",true));
            bufferedWriter.write(goods.getGoodsId()+","+goods.getGoodsName()+","+ goods.getPrice()+","+ goods.getGoodsNumber()+","+goods.getSellNumber());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，上架失败！");
            return false;
        }
    }
    public boolean buyGoodsByGoodsName(String goodsName,Goods goods){
        try {
            if (goods.getGoodsNumber()>0){
                goods.setSellNumber(goods.getSellNumber()+1);
                goods.setGoodsNumber(goods.getGoodsNumber()-1);
            }else
                return false;
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods>goodsList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Goods goods2=new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
               goodsList.add(goods2);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods2=goodsList.get(i);
                if (goods2.getGoodsName().equals(goodsName)){
                    goodsList.set(i,goods);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\Goods.txt"));
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods2=goodsList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",goods2.getGoodsId(),goods2.getGoodsName(),goods2.getPrice(),goods2.getGoodsNumber(),goods2.getSellNumber());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，购买失败！");
            return false;
        }

    }
    public boolean returnGoodsByGoodsName(String goodsName,Goods goods){
        try {
            if (goods.getSellNumber()>0){
                goods.setSellNumber(goods.getSellNumber()-1);
                goods.setGoodsNumber(goods.getGoodsNumber()+1);
            }else
                return false;
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods>goodsList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Goods goods2=new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods2);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods2=goodsList.get(i);
                if (goods2.getGoodsName().equals(goodsName)){
                    goodsList.set(i,goods);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\Goods.txt"));
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods2=goodsList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",goods2.getGoodsId(),goods2.getGoodsName(),goods2.getPrice(),goods2.getGoodsNumber(),goods2.getSellNumber());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，退货失败！");
            return false;
        }

    }
    public boolean changeGoodsByGoodsName(String goodsName,Goods goods){
        try {
            if (goods.getSellNumber()>0&&goods.getGoodsNumber()>0){
                goods.setSellNumber(goods.getSellNumber());
                goods.setGoodsNumber(goods.getGoodsNumber());
            }else
                return false;
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods>goodsList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Goods goods2=new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods2);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods2=goodsList.get(i);
                if (goods2.getGoodsName().equals(goodsName)){
                    goodsList.set(i,goods);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\Goods.txt"));
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods2=goodsList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",goods2.getGoodsId(),goods2.getGoodsName(),goods2.getPrice(),goods2.getGoodsNumber(),goods2.getSellNumber());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，换货失败！");
            return false;
        }
    }

    @Override
    public boolean deleteGoodsByGoodsId(String goodsId) {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods> goodsList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Goods goods=new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods = goodsList.get(i);
                if (goods.getGoodsId().equals(goodsId)) {
                    goodsList.remove(i);
                    break;
                }
            }
        BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\Goods.txt"));
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods=goodsList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",goods.getGoodsId(),goods.getGoodsName(),goods.getSellNumber(),goods.getGoodsNumber(),goods.getSellNumber());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，删除失败！");
            return false;
        }
    }

    @Override
    public boolean updateGoodsById(String goodsId, Goods goods) {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods> goodsList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Goods goods1=new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods1);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods1=goodsList.get(i);
               // if (goods1.getId().equals(id)){
                if (goods1.getGoodsId().equals(goodsId)){
                    goodsList.set(i,goods);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\Goods.txt"));
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods1=goodsList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",goods1.getGoodsId(),goods1.getGoodsName(),goods1.getPrice(),goods1.getGoodsNumber(),goods1.getSellNumber());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，更改失败！");
            return false;
        }
    }

    @Override
    public List<Goods> selectGoodsById(String goodsId) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods> goodsList = new ArrayList<>();
            List<Goods> goodsList1=new ArrayList<>();
            String string;
            while ((string = bufferedReader.readLine()) != null) {
                String[] dates = string.split(",");
                Goods goods1 = new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods1);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods1 = goodsList.get(i);
                if (goods1.getGoodsId().equals(goodsId)) {
                    goodsList1.add(goods1);
                }
            }
            bufferedReader.close();
            return goodsList1;
        }catch (IOException e){
            System.out.println("IO异常，查询失败！");
            return null;
        }
    }
    public Goods selectGoodsByGoodsName(String goodsName){
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods> goodsList = new ArrayList<>();
            List<Goods> goodsList1=new ArrayList<>();
            String string;
            while ((string = bufferedReader.readLine()) != null) {
                String[] dates = string.split(",");
                Goods goods1 = new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods1);
            }
            for (int i = 0; i < goodsList.size(); i++) {
                Goods goods1 = goodsList.get(i);
                if (goods1.getGoodsName().equals(goodsName)) {
                    bufferedReader.close();
                    return goods1;
                }
            }
            bufferedReader.close();
            return null;
        }catch (IOException e){
            System.out.println("IO异常，购买失败！");
            return null;
        }
    }

    @Override
    public List<Goods> selectAllGoods() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("SecondaryTradingSystem\\Goods.txt"));
            List<Goods> goodsList = new ArrayList<>();
            String string;
            while ((string = bufferedReader.readLine()) != null) {
                String[] dates = string.split(",");
                Goods goods = new Goods(dates[0],dates[1],Double.parseDouble(dates[2]),Integer.parseInt(dates[3]),Integer.parseInt(dates[4]));
                goodsList.add(goods);
            }
            bufferedReader.close();
            return goodsList;
        }catch (IOException e){
            System.out.println("IO异常，查询失败！");
            return null;
        }
    }
}

